<?php
    class LoginHandler{
				
		//A const to be used in the session to avoid strings
		const UserName = "UserName";
		
		//at this point we fake users and their pass in a UserDetails array
		private $UserDetails = array("Pelle"=>"pass", "Olle"=>"pass", "Kalle"=>"pass");
		
		/**
		 * @return boolean
		 * Function to check if user i logged in or not
		 */
    	public function isUserLoggedIn(){
		  	//Check if username has logged in	
		  	if (isset($_SESSION[LoginHandler::UserName]) == true) {
		      //its true, return it!
		      return TRUE;
		    } 
			//else return false
		    return FALSE;			
    	}
		
		/**
		 * @return Boolean
		 * funtion to check if user wants us to remember her/him
		 */
		public function isRememberBoxChecked($remember){
			//Check if remember me box is checked?
			if(isset($_SESSION[$remember]) == true){
				//If it is set, return true
				return true;
			}
			//Else return false
			return False;
		}
		
		/**
		 * @return boolean
		 * Function to LOGIN user, returns boolean to indicate succes
		 * Takes two arguments, username and password
		 */
		public function logIn($username, $password){
			//check username and password
			if(array_key_exists($username, $this->UserDetails) and $this->UserDetails[$username] == $password){
				//Store user in session
				$_SESSION[LoginHandler::UserName] = $username;
				return true;
			}
			return false;
			
		}
		
		/**
		 * Function to LOGOUT user
		 */
		public function logOut(){
			if(isset($_SESSION[LoginHandler::UserName])){
				unset($_SESSION[LoginHandler::UserName]);
				return true;
			}
			return false;
		}
    	
    	/**
		 * TEST AREA
		 * @bolean Testresult
		 * funtion to test LoginHandler class and its functions
		 * Return true or false and echos errorstring
		 */
		public function Test(){
			//Make sure we are not logged in	
			$this->logOut();
			
			//ISUSERLOGGEDIN function test when logged OUT
			if($this->isUserLoggedIn()){
				//If this returnes true, we have an error in isUserLoggedIn or logOut	
				echo "isUserLoggedIn returns 'True' after the logout() function has been used";
				return false;
			}
			
			//LOGIN function test
				//Incorrect userdata
			$username = "noUser";
			$password = "noPass";
			if($this->logIn($username, $password)){
				echo "logIn returns true when given incorrect userdata";
				return false;
			}
			
				//Incorrect userName and correct password
			$username = "noUser";
			$password = "pass";
			if($this->logIn($username, $password)){
				echo "logIn returns true when given incorrect userdata";
				return false;
			}
			
				//Incorrect password and correct username
			$username = "Olle";
			$password = "noPass";
			if($this->logIn($username, $password)){
				echo "logIn returns true when given incorrect userdata";
				return false;
			}
			
				//Correct userdata			
			if(!$this->logIn("Olle", "pass")){
				//If this returns false we have an error in the logIn function	
				echo "logIn returns false when given correct userdata";
				return false;
			}
			
			//ISUSERLOGGEDIN function test when logged IN
			if(!$this->isUserLoggedIn()){
				//If this returnes false, we have an error in isUserLoggedIn or logIn	
				echo "isUserLoggedIn returns 'False' after the logIn() function has been used";
				return false;
			}
			
			//LOGOUT function test
			if(!$this->logOut()){
				//If this returns false we have an error in the logOut function
				echo "logOut returns 'False' when we try to logout when we are logged in";
				return false;
			}
			
			
			
			//If we get to this point our shit is good!!
			return true;
		}
		
    }
